package com.ruoyi.material.controller;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.project.service.IProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.Supplier;
import com.ruoyi.material.service.ISupplierService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商Controller
 *
 * @author Q
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/material/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IProjectService projectService;



    /**
     * 查询供应商列表
     */
    @PreAuthorize("@ss.hasPermi('material:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list( Supplier supplier) {
        startPage();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        List<Supplier> list = supplierService.selectSupplierList(supplier, userId);
        return getDataTable(list);
    }
    /**
     * 查询供应商列表 不分页
     */
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        List<Supplier> list = supplierService.selectSupplierListAll(userId);
        return success(list);
    }

//    @@@@@@@@@@@@@@@@@@@@@@@@@
//    测试上传
//    @@@@@@@@@@@@@@@@@@@@@@@@@

//    /**
//     * 导出供应商列表
//     */
//    @PreAuthorize("@ss.hasPermi('material:supplier:export')")
//    @Log(title = "供应商", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Supplier supplier) {
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        Long userId = loginUser.getUserId();
//        List<Supplier> list = supplierService.selectSupplierList(supplier, userId);
//        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
//        util.exportExcel(response, list, "供应商数据");
//    }

    /**
     * 获取供应商详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:supplier:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId) {
        return success(supplierService.selectSupplierBySupplierId(supplierId));
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('material:supplier:add')")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Supplier supplier) {
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('material:supplier:edit')")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier) {
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除供应商
     */
    @PreAuthorize("@ss.hasPermi('material:supplier:remove')")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds) {
        return toAjax(supplierService.deleteSupplierBySupplierIds(supplierIds));
    }
}
